<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\articlemanager\Category;
use app\common\model\articlemanager\Article as ArticleModel;
use think\Db;
use think\Config;
use fast\Random;

/**
 * 文章接口
 */
class Article extends Api
{
    protected $noNeedLogin = ['articleList', 'articleInfo', 'categoryList'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     *
     */
    public function index()
    {
        $this->success('', ['welcome' => '123']);
    }

    /*
     * 文章列表
     */
    public function articleList(){
        $category_id = input('category_id/d', 0);
        $where['status'] = 0;
        if($category_id > 0){
            $where['category_id'] = $category_id;
        }

        //$field = 'id, name, introduce, price, sale_num_show, gold_num, category_id';
        //->field($field)
        $list_row = 15;
        if(in_array($category_id, array(1, 2))){
            $list_row_help = ArticleModel::where($where)->count();
            $list_row = $list_row_help > $list_row ? $list_row_help : $list_row;
        }
        $order = 'weigh DESC';
        $article_list = ArticleModel::where($where)->order($order)->paginate($list_row);

        $this->success('文章列表', $article_list);
    }

    /*
     * 文章信息
     */
    public function articleInfo(){
        $article_id = input('id/d', 0);
        if($article_id <= 0){
            $this->error('参数有误');
        }
        $where['id'] = $article_id;
        $where['status'] = 0;
        //$field = 'id, name, category_id, introduce, content, price, sale_num_show, display, gold_num';
        //->field($field)
        $article_info = ArticleModel::where($where)->find();
        if(!$article_info){
            $this->error('文章不存在');
        }
        // 对输入的文本处理
        $article_info['content'] = richTextAbsoluteUrl($article_info['content']);
        $this->success('文章信息', $article_info);
    }

    /*
     * 文章分类
     */
    public function categoryList(){
        $list = Category::order('weigh DESC')->select();
        $this->success('文章分类', $list);
    }
   
}